﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OnllyPage2UIController : MonoBehaviour {

    public static OnllyPage2UIController _instance;
    public bool b_isEnterClick;        //是否按了"Enter"键

    private int[,] arr = new int[12, 3];//tf_middle的存储
    private int i;                      //行（0开始）
    private int j;                      //列（0开始）

    public Sprite s_open;             //
    public Sprite s_close;
    public Transform tf_bottomUI;
    public Transform tf_page2;        //page2界面
    public float f_dianliu=0.500f;          //电流的变化量
    private float f_shijian=0.200f;          //时间的变化量
    private float f_endDianliu=9.000f;       //电流的最大值
    private float f_endDianliuTemp=9.000f;   //电流变化的最终值
    private float f_startDianliu=3.000f;     //电流的起始值
    public Text t_dianliuValue;       //随着时间变化的电流
    public bool b_isStart;           //判断是否开始实验
    private float f_tempTime;         //实验开始后时间的变化量
    private float f_tempDianliu=3.000f;      //实验开始后电流的变化量
    public Transform tf_allPage;      //所有页面的父集
    private string s_temp;            //可输入数字的字符串
    private float f_temp;             //可输入数字的浮点型（用于转化为4位数的字符串4位数）
    private string s_dianliuUpORDown = "up";  //判断电流是+或者-
    private string test = "始->终";     //默认电流的变化方式是“始->终”
    public string testType = "程控"; 

    private int i_pageNumTemp;
    public Transform tf_duihuakuang;
    public bool b_shoukongStart;          //手控试验是否开始

    public GameObject go_chengkongSV;     //程控的事件记录
    public GameObject go_shoukongSV;      //手控的事件记录
    public GameObject go_shoukong;        //手控UI

    // Use this for initializatio
    void Start () {
        _instance = this;
        EventManager<string>.AddEvent(MyEventType.OnllyDirButtonClickEvent, OnDirButtonClick);
        EventManager<string>.AddEvent(MyEventType.XuanzhuanAnniuEvent, OnXuanzhuanAnniu);
        EventManager<string>.AddEvent(MyEventType.OnllyNumberButtonClickEvent, numController);
        EventManager<bool>.AddEvent(MyEventType.OnllyEnterButtonClickEvent, OnEnterButtonClick);
        EventManager<bool>.AddEvent(MyEventType.OnllyStartButtonClickEvent, OnStartButtonClick);
        EventManager<string>.AddEvent(MyEventType.OnllyDirButtonClickEvent,OnDuihuakuangDir);
        EventManager<bool>.AddEvent(MyEventType.OnllyEnterButtonClickEvent,OnDuihuakuangEnter);
        EventManager<bool>.AddEvent(MyEventType.OnllyReSetPageEvent, ReSet);
        Init();
    }

    void ChangePage2UIByTestType(string type) {
        switch (type) {
            case "程控":
                ChengkongUI();break;
            case "手控":
                ShoukongUI();break;
            default:break;
        }
    }

    void ShoukongUI()
    {
        for (int i = 5; i <= 9; i++)
            for (int j = 0; j <= 1; j++) {
                string name = i + "_" + j;
                OnllyUIController._instance.ChangeType(tf_page2, 3, name);
            }
        for (int i = 1; i <= 2; i++)
            for (int j = 0; j <= 2; j++) {
                string name = i + "_" + j;
                OnllyUIController._instance.ChangeType(tf_page2, 0, name);
            }
        OnllyUIController._instance.ChangeType(tf_page2, 3, "5_2");
        tf_page2.Find("5_1").GetChild(2).GetComponent<Text>().color = new Color32(15, 31, 38, 70);
        tf_page2.Find("5_2").GetChild(2).GetComponent<Text>().color = new Color32(15, 31, 38, 70);
        tf_page2.Find("7_1").GetChild(2).GetComponent<Text>().color = new Color32(15, 31, 38, 70);
    }

    void ChengkongUI() {
        for (int i = 5; i <= 9; i++)
            for (int j = 0; j <= 1; j++)
            {
                string name = i + "_" + j;
                OnllyUIController._instance.ChangeType(tf_page2, 0, name);
            }
        for (int i = 1; i <= 2; i++)
            for (int j = 0; j <= 2; j++)
            {
                string name = i + "_" + j;
                OnllyUIController._instance.ChangeType(tf_page2, 3, name);
            }
        OnllyUIController._instance.ChangeType(tf_page2, 0, "5_2");
        OnllyUIController._instance.ChangeType(tf_page2, 2, "9_1");
        tf_page2.Find("5_1").GetChild(2).GetComponent<Text>().color = new Color32(15, 31, 38, 255);
        tf_page2.Find("5_2").GetChild(2).GetComponent<Text>().color = new Color32(15, 31, 38, 255);
        tf_page2.Find("7_1").GetChild(2).GetComponent<Text>().color = new Color32(15, 31, 38, 255);
    }

    private void ReSet(bool obj)
    {
        if (OnllyUIController._instance.i_pageNum == 1)
        {
            string name = i + "_" + j;
            JudgeInput(name, false);
            i = 0;
            j = 0;
            name = i + "_" + j;
            JudgeInput(name, true);
        }
    }

    private void OnDirButtonClick(string name)
    {
        if (OnllyUIController._instance.i_pageNum == 1 && !b_isStart && !OnllyUIController._instance.b_isDuihuakuang) {
            switch (name)
            {
                case "up":
                    OnUpButtonClick(true); break;
                case "down":
                    OnDownButtonClick(true); break;
                case "left":
                    OnLeftButtonClick(true); break;
                case "right":
                    OnRightButtonClick(true); break;
                default: break;
            }
        }
    }

    void Update() {
        //开始实验
        if (b_isStart) {
            f_tempTime += Time.deltaTime;
            if (f_tempTime >= f_shijian) {
                f_tempTime = 0;
                OnllyUIController._instance.ChangeZhishideng(true, f_shijian); //指示灯亮红
                if (s_dianliuUpORDown == "up")
                {
                    if (f_tempDianliu + f_dianliu >= f_endDianliuTemp)//最终电流不能大于设定的最大值
                        f_tempDianliu = f_endDianliuTemp;
                    else
                        f_tempDianliu += f_dianliu;
                    if (f_tempDianliu >= f_endDianliuTemp)
                    {
                        geduanDianliu();
                        changZhishideng();
                        EventManager<bool>.TriggerEvent(MyEventType.ChangeUIToShijianjiluEvent, true);//实验结束后“四方”设备界面跳转到事件记录界面
                        ShijianJiluUIController._instance.t_date.GetComponent<Text>().text = System.DateTime.Now.ToString("MM - dd   HH : mm : fff");
                        ShijianJiluUIController._instance.b_canStart = false;
                        b_isStart = false;
                        TimerMngr.Instance.Create(() => {
                            tf_bottomUI.Find("switch").GetChild(0).GetComponent<Image>().sprite = s_close;
                            ChangeMainPage(4);                          //切换onlly当前页面到第5页
                            EventManager<bool>.TriggerEvent(MyEventType.OnllyResultEvent, true);
                            //OnllyUIController._instance.i_shiyancishu++;//每次试验完后，试验的次数+1
                        }).Start(0.5f);
                    }
                }
                else if (s_dianliuUpORDown == "down") {                
                    if (f_tempDianliu - f_dianliu <= f_endDianliuTemp)//最终电流不能小于设定的最小值
                        f_tempDianliu = f_endDianliuTemp;
                    else
                        f_tempDianliu -= f_dianliu;
                    if (f_tempDianliu <= f_endDianliuTemp)
                    {
                        geduanDianliu();
                        changZhishideng();
                        EventManager<bool>.TriggerEvent(MyEventType.ChangeUIToShijianjiluEvent, true);
                        ShijianJiluUIController._instance.t_date.GetComponent<Text>().text = System.DateTime.Now.ToString("MM - dd   HH : mm : fff");
                        ShijianJiluUIController._instance.b_canStart = false;
                        b_isStart = false;
                        TimerMngr.Instance.Create(() => {
                            tf_bottomUI.Find("switch").GetChild(0).GetComponent<Image>().sprite = s_close;
                            ChangeMainPage(4);  //切换当前页面到第5页
                            EventManager<bool>.TriggerEvent(MyEventType.OnllyResultEvent, true);
                            //OnllyUIController._instance.i_shiyancishu++;
                        }).Start(0.5f);  
                    }
                }
                if (isDayuTen(f_tempDianliu)) {
                    t_dianliuValue.text = f_tempDianliu.ToString("0.00");
                }else
                    t_dianliuValue.text = f_tempDianliu.ToString("0.000");
            }          
        }
    }
    /// <summary>
    /// “1”可进行数字输入  
    /// "-1"空白的区域
    /// "3"背景、字体的颜色是灰色的
    /// </summary>
    void Init() {
        arr[0,2] = 1;
        arr[5,1] = 1;
        arr[5,2] = 1;
        arr[7,1] = 1;
        for (int k = 3; k <= 11; k++) {
            if (k != 5) {
                arr[k, 2] = -1;
            } 
        }
        for (int i = 1; i <= 2; i++) {
            for (int j = 0; j <= 2; j++) {
                arr[i, j] = 3;
            }
        }
        for (int i = 10; i <= 11; i++) {
            for (int j = 0; j <= 1; j++) {
                arr[i, j] = 3;
            }
        }        
    }

    /// <summary>
    /// 改变指示灯的状态
    /// </summary>
    void changZhishideng() {
        for (int index = 1; index <= 3; index++) {
            Unit unit = new Unit(index,"dongzuo");
            EventManager<Unit>.TriggerEvent(MyEventType.ZhishidengTypeEvent,unit);
        }
    }

    /// <summary>
    /// 数字键+“.”+“-”
    /// </summary>
    /// <param name="num"></param>
    private void numController(string num) {
        if (OnllyUIController._instance.i_pageNum == 1 && !b_isStart) {
            if (b_isEnterClick)
            {
                string name = i + "_" + j;
                if (num == ".")
                {
                    if (!tf_page2.Find(name).GetChild(1).GetComponent<Text>().text.Contains(".")) {//只能有一个“.”
                        tf_page2.Find(name).GetChild(1).GetComponent<Text>().text += num;
                    }
                }
                else if (num == "-")//删除最后一个数字
                {
                    string temp = tf_page2.Find(name).GetChild(1).GetComponent<Text>().text;
                    if (temp.Length >= 1)
                    {
                        temp = temp.Substring(0, temp.Length - 1);
                        tf_page2.Find(name).GetChild(1).GetComponent<Text>().text = temp;
                        s_temp = tf_page2.Find(name).GetChild(1).GetComponent<Text>().text;
                    }
                }
                else
                {
                    for (int temp = 0; temp <= 9; temp++)
                    {
                        if (int.Parse(num) == temp)
                        {
                            tf_page2.Find(name).GetChild(1).GetComponent<Text>().text += num;
                            s_temp = tf_page2.Find(name).GetChild(1).GetComponent<Text>().text;
                            break;
                        }
                    }
                }    
            }
        } 
    }

    /// <summary>
    /// 可输入跟不可输入（由于可输入的跟不可输入的子物体不同，在这分开处理）
    /// </summary>
    /// <param name="name">方框的名字</param>
    void JudgeInput(string name,bool active) {
        if (active) {
            if (i == 0 && j == 2) {
                tf_page2.Find(name).GetChild(2).GetComponent<Text>().color = Color.white;
            }
            if ((i == 5 && j == 1) || (i == 5 && j == 2) || (i == 7 && j == 1))
            {
                tf_page2.Find(name).GetChild(2).GetComponent<Text>().color = Color.white;
            }
            if (j==0)//redBG
            {
                OnllyUIController._instance.ChangeType(tf_page2, 4, name);
            }
            else//blackBG
            {
                OnllyUIController._instance.ChangeType(tf_page2, 5, name);
            }
        }
        else
        {
            if (true)
            {
                if (i == 0 && j == 2)
                {
                    tf_page2.Find(name).GetChild(2).GetComponent<Text>().color = new Color32(200, 38, 84, 255);
                }
                if ((i == 5 && j == 1) || (i == 5 && j == 2) || (i == 7 && j == 1))
                {
                    tf_page2.Find(name).GetChild(2).GetComponent<Text>().color = new Color32(15, 31, 38, 255);
                }
                if ((i == 0 && j == 1) || (i == 0 && j == 2))//red
                {
                    OnllyUIController._instance.ChangeType(tf_page2, 1, name);
                }
                else if ((i == 4 && j == 1) || (i == 9 && j == 1))//blue
                {
                    OnllyUIController._instance.ChangeType(tf_page2, 2, name);
                }
                else if (arr[i, j] == 3)//hui
                {
                    OnllyUIController._instance.ChangeType(tf_page2, 3, name);
                }
                else//black
                {
                    OnllyUIController._instance.ChangeType(tf_page2, 0, name);
                }
                if (testType == "手控") {
                    ShoukongUI();
                }
            }
        } 
    }

    /// <summary>
    /// 到可输入的文本框后  只按了一次Enter后接着按方向键后 该文本框的值为默认值或是输到“一半”的值
    /// </summary>
    void UpdateNum(string name) {
        if (arr[i, j] == 1 && b_isEnterClick)
        {
            b_isEnterClick = false;
            //单位显示
            tf_page2.Find(name).GetChild(2).gameObject.SetActive(true);
            tf_page2.Find(name).Find("guangbiao").gameObject.SetActive(false) ;//光标消失
            f_temp = float.Parse(s_temp);
            if (isDayuTen(f_temp)) {
                tf_page2.Find(name).GetChild(1).GetComponent<Text>().text = f_temp.ToString("0.00");
            }else
                tf_page2.Find(name).GetChild(1).GetComponent<Text>().text = f_temp.ToString("0.000"); ;
            tf_page2.Find(name).GetChild(1).GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
            if (i == 7)//时间
            {
                f_shijian = f_temp;
            }
            else//电流
            {
                if (i == 0 && j == 2)//电流的变化量
                {
                    f_dianliu = f_temp;
                }
                else if (i == 5 && j == 1)//起始电流
                {
                    f_startDianliu = f_temp;
                }
                else if (i == 5 && j == 2)//最终电流
                {
                    f_endDianliu = f_temp;
                }
            }
        }
    }

    void OnUpButtonClick(bool active) {
        if (i >= 0) {
            string name = "";
            name = i + "_" + j;
            UpdateNum(name);
            JudgeInput(name,false);
            i--;
            if (i == -1)
            {
                if (j == 2)
                {
                    i = 5;
                }
                else {
                    i = 11;
                }
            }else
            if (i == 4 && j == 2) {
                i = 2;
            }else
            if (arr[i, j] == -1)
            {
                i++;
            }
            name = i + "_" + j;
            JudgeInput(name,true);
        }
    }

    void OnDownButtonClick(bool active) {
        if (i <= 11) {
            string name = "";
            name = i + "_" + j;
            UpdateNum(name);
            JudgeInput(name, false);
            i++;
            if (i == 12)
            {
                i = 0;
            }
            else if (i == 6 && j == 2) {
                i = 0;
            }
            else
            if (i == 3 && j == 2)
            {
                i = 5;
            }
            else
            if (arr[i, j] == -1)
            {
                i--;
            }
            name = i + "_" + j;
            JudgeInput(name, true);
        }
    }

    void OnLeftButtonClick(bool active) {
        if (j >= 0) {
            string name = "";
            name = i + "_" + j;
            UpdateNum(name);
            JudgeInput(name, false);
            j--;
            if (j == -1) {
                j = 2;
                if (arr[i, j] == -1)
                {
                    j = 1;
                }
                else {
                    j = 2;
                }
            }else
            if (arr[i, j] == -1)//空白区域
            {
                j++;
            }
            name = i + "_" + j;
            JudgeInput(name, true);
        }
    }

    void OnRightButtonClick(bool active) {
        if (j <= 2)
        {
            string name = "";
            name = i + "_" + j;
            UpdateNum(name);
            JudgeInput(name, false);
            j++;
            if (j == 3) {
                j = 0;
            }else if (arr[i, j] == -1) {//空白区域
                j=0;
            }
            name = i + "_" + j;
            JudgeInput(name, true);
        }
    }

    /// <summary>
    /// Enter键
    /// </summary>
    private void OnEnterButtonClick(bool active)
    {
        string name = "";
        name = i + "_" + j;
        if (i == 4 && j == 1 && !OnllyUIController._instance.b_isDuihuakuang && OnllyUIController._instance.i_pageNum == 1)
        {//Enter后改变程控/手控的类型
            if (tf_page2.Find(name).GetChild(1).GetComponent<Text>().text == "程控")
            {
                tf_page2.Find(name).GetChild(1).GetComponent<Text>().text = "手控";
                testType = "手控";
                go_shoukong.SetActive(true);
                ChangePage2UIByTestType(testType);
            }
            else
            {
                tf_page2.Find(name).GetChild(1).GetComponent<Text>().text = "程控";
                testType = "程控";
                go_shoukong.SetActive(false);
                ChangePage2UIByTestType(testType);
            }
            OnllyUIController._instance.ChangeZhishideng(false, 0.5f);
        }else
        if (i == 6 && j == 1 && !OnllyUIController._instance.b_isDuihuakuang && OnllyUIController._instance.i_pageNum == 1 && testType == "程控")
        {//Enter后改变"变化方式”的类型
            if (tf_page2.Find(name).GetChild(1).GetComponent<Text>().text == "始->终")
            {
                tf_page2.Find(name).GetChild(1).GetComponent<Text>().text = "终->始";
                test = "终->始";
            }
            else
            {
                tf_page2.Find(name).GetChild(1).GetComponent<Text>().text = "始->终";
                test = "始->终";
            }
            OnllyUIController._instance.ChangeZhishideng(false, 0.5f);
        }else if (arr[i, j] == 1 && !b_isEnterClick && OnllyUIController._instance.i_pageNum == 1 && !OnllyUIController._instance.b_isDuihuakuang)
        {
            OnllyUIController._instance.ChangeZhishideng(false, 0.5f);
            if (testType == "手控" && i == 0 && j == 2)//如果是手控的话第二页输入框只有0_2能输入
            {
                tf_page2.Find(name).Find("guangbiao").gameObject.SetActive(true);//光标出现
                OnllyUIController._instance.ChangeType(tf_page2, 0, name);
                //Enter后记录下当前文本框的text
                s_temp = tf_page2.Find(name).GetChild(1).GetComponent<Text>().text;
                if (s_temp == "")
                {
                    s_temp = "0.000";
                }
                //数字居左
                tf_page2.Find(name).GetChild(1).GetComponent<Text>().alignment = TextAnchor.MiddleLeft;
                //有数据的话清除原先的数据
                tf_page2.Find(name).GetChild(1).GetComponent<Text>().text = "";
                //单位隐藏
                tf_page2.Find(name).GetChild(2).gameObject.SetActive(false);
                //高亮隐藏
                tf_page2.Find(name).GetChild(0).gameObject.SetActive(false);
                //数字键输入数字
                //Enter
                b_isEnterClick = true;
            }
            else if(testType == "程控") {
                tf_page2.Find(name).Find("guangbiao").gameObject.SetActive(true);//光标出现
                OnllyUIController._instance.ChangeType(tf_page2, 0, name);
                //Enter后记录下当前文本框的text
                s_temp = tf_page2.Find(name).GetChild(1).GetComponent<Text>().text;
                if (s_temp == "")
                {
                    s_temp = "0.000";
                }
                //数字居左
                tf_page2.Find(name).GetChild(1).GetComponent<Text>().alignment = TextAnchor.MiddleLeft;
                //有数据的话清除原先的数据
                tf_page2.Find(name).GetChild(1).GetComponent<Text>().text = "";
                //单位隐藏
                tf_page2.Find(name).GetChild(2).gameObject.SetActive(false);
                //高亮隐藏
                tf_page2.Find(name).GetChild(0).gameObject.SetActive(false);
                //数字键输入数字
                //Enter
                b_isEnterClick = true;
            }
            
        }
        else if (arr[i, j] == 1 && b_isEnterClick && !OnllyUIController._instance.b_isDuihuakuang)
        {
            OnllyUIController._instance.ChangeZhishideng(false, 0.5f);
            tf_page2.Find(name).Find("guangbiao").gameObject.SetActive(false);//光标消失
            tf_page2.Find(name).GetChild(2).GetComponent<Text>().color = Color.white;
            OnllyUIController._instance.ChangeType(tf_page2, 5, name);
            b_isEnterClick = false;
            s_temp = tf_page2.Find(name).GetChild(1).GetComponent<Text>().text;
            //单位显示
            tf_page2.Find(name).GetChild(2).gameObject.SetActive(true);
            //高亮显示
            tf_page2.Find(name).GetChild(0).gameObject.SetActive(true);
            //数字居中
            if (i == 7)//时间
            {
                if (tf_page2.Find(name).GetChild(1).GetComponent<Text>().text == "")
                {
                    f_shijian = 0.000f;
                }
                else
                    f_shijian = float.Parse(tf_page2.Find(name).GetChild(1).GetComponent<Text>().text);
            }
            else
            {//电流
                if (i == 0 && j == 2)//电流的变化量
                {
                    if (tf_page2.Find(name).GetChild(1).GetComponent<Text>().text == "")
                    {
                        f_dianliu = 0.000f;
                    }
                    else
                        f_dianliu = float.Parse(tf_page2.Find(name).GetChild(1).GetComponent<Text>().text);
                }
                else if (i == 5 && j == 1)//起始电流
                {
                    if (tf_page2.Find(name).GetChild(1).GetComponent<Text>().text == "")
                    {
                        f_startDianliu = 0.000f;
                    }
                    else
                        f_startDianliu = float.Parse(tf_page2.Find(name).GetChild(1).GetComponent<Text>().text);
                }
                else if (i == 5 && j == 2)
                {//最终电流
                    if (tf_page2.Find(name).GetChild(1).GetComponent<Text>().text == "")
                    {
                        f_endDianliu = 0.000f;
                    }
                    else
                        f_endDianliu = float.Parse(tf_page2.Find(name).GetChild(1).GetComponent<Text>().text);
                }
            }
            if (s_temp == "")
            {
                s_temp = "0.000";
            }
            f_temp = float.Parse(s_temp);
            if (isDayuTen(f_temp))
            {
                tf_page2.Find(name).GetChild(1).GetComponent<Text>().text = f_temp.ToString("0.00");
            }
            else
                tf_page2.Find(name).GetChild(1).GetComponent<Text>().text = f_temp.ToString("0.000");
            tf_page2.Find(name).GetChild(1).GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
        }
    }

    /// <summary>
    /// 实验结束后的各段电流
    /// </summary>
    void geduanDianliu()
    {
        if (test == "始->终")
        {
            if (f_startDianliu < f_endDianliu)
            {
                //1-3段的电流（动作电流+延迟电流）
                float temp1 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(11))+ ShijianJiluUIController._instance.f_IduanTime / f_shijian * f_dianliu;
                float temp2 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(13)) + ShijianJiluUIController._instance.f_IIduanTime / f_shijian * f_dianliu;
                float temp3 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(15)) + ShijianJiluUIController._instance.f_IIIduanTime / f_shijian * f_dianliu;
                if (isDayuTen(temp1)) {
                    ShijianJiluUIController._instance.tf_IduanDongzuo.GetChild(0).GetComponent<Text>().text = temp1.ToString("0.00") + "A    0.011A    0.002A";
                }else
                    ShijianJiluUIController._instance.tf_IduanDongzuo.GetChild(0).GetComponent<Text>().text = temp1.ToString("0.000") + "A    0.011A    0.002A";
                if (isDayuTen(temp2)) {
                    ShijianJiluUIController._instance.tf_IIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp2.ToString("0.00") + "A    0.002A    0.000A";
                }else
                    ShijianJiluUIController._instance.tf_IIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp2.ToString("0.000") + "A    0.002A    0.000A";
                if (isDayuTen(temp3)) {
                    ShijianJiluUIController._instance.tf_IIIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp3.ToString("0.00") + "A    0.008A    0.001A";
                }else
                    ShijianJiluUIController._instance.tf_IIIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp3.ToString("0.000") + "A    0.008A    0.001A";
            }
            else {
                float temp1 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(11)) - ShijianJiluUIController._instance.f_IduanTime / f_shijian * f_dianliu;
                float temp2 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(13)) - ShijianJiluUIController._instance.f_IIduanTime / f_shijian * f_dianliu;
                float temp3 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(15)) - ShijianJiluUIController._instance.f_IIIduanTime / f_shijian * f_dianliu;
                if (temp1 <= 0)
                {
                    temp1 = 0;
                }
                if (temp2 <= 0)
                {
                    temp2 = 0;
                }
                if (temp3 <= 0)
                {
                    temp3 = 0;
                }
                if (isDayuTen(temp1))
                {
                    ShijianJiluUIController._instance.tf_IduanDongzuo.GetChild(0).GetComponent<Text>().text = temp1.ToString("0.00") + "A    0.011A    0.002A";
                }
                else
                    ShijianJiluUIController._instance.tf_IduanDongzuo.GetChild(0).GetComponent<Text>().text = temp1.ToString("0.000") + "A    0.011A    0.002A";
                if (isDayuTen(temp2))
                {
                    ShijianJiluUIController._instance.tf_IIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp2.ToString("0.00") + "A    0.002A    0.000A";
                }
                else
                    ShijianJiluUIController._instance.tf_IIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp2.ToString("0.000") + "A    0.002A    0.000A";
                if (isDayuTen(temp3))
                {
                    ShijianJiluUIController._instance.tf_IIIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp3.ToString("0.00") + "A    0.008A    0.001A";
                }
                else
                    ShijianJiluUIController._instance.tf_IIIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp3.ToString("0.000") + "A    0.008A    0.001A";
            }   
        }
        else if(test=="终->始"){
            if (f_startDianliu < f_endDianliu)
            {
                float temp1 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(11)) - ShijianJiluUIController._instance.f_IduanTime / f_shijian * f_dianliu;
                float temp2 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(13)) - ShijianJiluUIController._instance.f_IIduanTime / f_shijian * f_dianliu;
                float temp3 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(15)) - ShijianJiluUIController._instance.f_IIIduanTime / f_shijian * f_dianliu;
                if (temp1 <= 0)
                {
                    temp1 = 0;
                }
                if (temp2 <= 0)
                {
                    temp2 = 0;
                }
                if (temp3 <= 0)
                {
                    temp3 = 0;
                }
                if (isDayuTen(temp1))
                {
                    ShijianJiluUIController._instance.tf_IduanDongzuo.GetChild(0).GetComponent<Text>().text = temp1.ToString("0.00") + "A    0.011A    0.002A";
                }
                else
                    ShijianJiluUIController._instance.tf_IduanDongzuo.GetChild(0).GetComponent<Text>().text = temp1.ToString("0.000") + "A    0.011A    0.002A";
                if (isDayuTen(temp2))
                {
                    ShijianJiluUIController._instance.tf_IIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp2.ToString("0.00") + "A    0.002A    0.000A";
                }
                else
                    ShijianJiluUIController._instance.tf_IIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp2.ToString("0.000") + "A    0.002A    0.000A";
                if (isDayuTen(temp3))
                {
                    ShijianJiluUIController._instance.tf_IIIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp3.ToString("0.00") + "A    0.008A    0.001A";
                }
                else
                    ShijianJiluUIController._instance.tf_IIIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp3.ToString("0.000") + "A    0.008A    0.001A";
            }
            else {
                float temp1 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(11)) + ShijianJiluUIController._instance.f_IduanTime / f_shijian * f_dianliu;
                float temp2 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(13)) + ShijianJiluUIController._instance.f_IIduanTime / f_shijian * f_dianliu;
                float temp3 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(15)) + ShijianJiluUIController._instance.f_IIIduanTime / f_shijian * f_dianliu;
                if (isDayuTen(temp1))
                {
                    ShijianJiluUIController._instance.tf_IduanDongzuo.GetChild(0).GetComponent<Text>().text = temp1.ToString("0.00") + "A    0.011A    0.002A";
                }
                else
                    ShijianJiluUIController._instance.tf_IduanDongzuo.GetChild(0).GetComponent<Text>().text = temp1.ToString("0.000") + "A    0.011A    0.002A";
                if (isDayuTen(temp2))
                {
                    ShijianJiluUIController._instance.tf_IIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp2.ToString("0.00") + "A    0.002A    0.000A";
                }
                else
                    ShijianJiluUIController._instance.tf_IIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp2.ToString("0.000") + "A    0.002A    0.000A";
                if (isDayuTen(temp3))
                {
                    ShijianJiluUIController._instance.tf_IIIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp3.ToString("0.00") + "A    0.008A    0.001A";
                }
                else
                    ShijianJiluUIController._instance.tf_IIIduanDongzuo.GetChild(0).GetComponent<Text>().text = temp3.ToString("0.000") + "A    0.008A    0.001A";
            }
            
        }
    }
    /// <summary>
    /// 判断得到的数字是否>=10
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    private bool isDayuTen(float num)
    {
        if (num >= 10.0f)
        {
            return true;
        }
        return false;
    }
    /// <summary>
    /// 退出
    /// </summary>
    void OnEscButtonClick() {

    }

    /// <summary>
    /// 是否把变化电流、变化范围、变化时间都输入了
    /// </summary>
    bool isInput() {
        string[] name = new string[4] {"0_2","5_1","5_2","7_1"};
        int RightNum = 0;
        for (int i = 0; i < name.Length; i++) {
            if (tf_page2.Find(name[i]).GetChild(1).GetComponent<Text>().text != "") {
                RightNum++;
            }
        }
        if (RightNum == 4) {
            return true;
        }
        return false;
    }
    
    /// <summary>
    /// 点击开始出现是否正式开始试验弹框
    /// 总用时： （i_endDianliu-i_startDianliu）* i_shijian/i_dianliu
    /// </summary>
    void OnStartButtonClick(bool active) {
        if (!b_isStart && ShijianJiluUIController._instance.b_canStart && !OnllyUIController._instance.b_isDuihuakuang && !b_isEnterClick && isInput() && is_AllTouru() && !b_shoukongStart) {
            tf_bottomUI.Find("tishi").gameObject.SetActive(true) ;
            tf_bottomUI.Find("isStart").gameObject.SetActive(true);
            //i_pageNumTemp = OnllyUIController._instance.i_pageNum;  //记录当前界面
            OnllyUIController._instance.b_isDuihuakuang = true;       //此时只有对话框可以交互
        } 
    }

    /// <summary>
    /// 是否正式开始试验对话框的左右操作
    /// </summary>
    /// <param name="dir"></param>
    void OnDuihuakuangDir(string dir) {
        if (OnllyUIController._instance.b_isDuihuakuang) {
            switch (dir) {
                case "left":
                    tf_duihuakuang.GetChild(0).GetChild(0).gameObject.SetActive(true);
                    tf_duihuakuang.GetChild(0).GetChild(1).GetComponent<Text>().color=Color.white;
                    tf_duihuakuang.GetChild(1).GetChild(0).gameObject.SetActive(false);
                    tf_duihuakuang.GetChild(1).GetChild(1).GetComponent<Text>().color = new Color32(15,31,38,255);
                    break;
                case "right":
                    tf_duihuakuang.GetChild(0).GetChild(0).gameObject.SetActive(false);
                    tf_duihuakuang.GetChild(0).GetChild(1).GetComponent<Text>().color = new Color32(15, 31, 38, 255);
                    tf_duihuakuang.GetChild(1).GetChild(0).gameObject.SetActive(true);
                    tf_duihuakuang.GetChild(1).GetChild(1).GetComponent<Text>().color = Color.white;
                    break;
                default:break;
            }
        }
    }

    /// <summary>
    /// 是否正式开始试验对话框的Enter操作
    /// </summary>
    /// <param name="active"></param>
    void OnDuihuakuangEnter(bool active) {
        if (OnllyUIController._instance.b_isDuihuakuang) {
            if (tf_duihuakuang.GetChild(0).GetChild(0).gameObject.activeSelf  && !b_shoukongStart)
            {
                if (testType == "程控")
                    OnQueRenButtonClick();
                else
                    OnQueRenButtonClickShouKong();
            }
            else {
                OnDuihuakuangDir("left");
                OnQuXiaoButtonClick();
            }
        }
    }

    /// <summary>
    /// 是否正式开始试验确认按钮
    /// </summary>
    void OnQueRenButtonClick() {
        OnllyPage5UIController.Instance.ClearJieGuo();
        OnllyPage1UIController.Instance.tf_right.Find("dongzuozhi").GetComponent<Text>().text = "";  //清空上一个试验的结果数据
        OnllyPage1UIController.Instance.tf_right.Find("fanhuizhi").GetComponent<Text>().text = "";
        OnllyPage1UIController.Instance.tf_right.Find("fanhuixishu").GetComponent<Text>().text = "";
        go_chengkongSV.SetActive(true);
        go_shoukongSV.SetActive(false);
        ShijianJiluUIController._instance.GetDianliu();   //得到每段電流的過流時間
        if (test == "始->终")
        {
            if (f_startDianliu < f_endDianliu)            //电流变化范围（f_startDianliu<f_endDianliu）
            {
                s_dianliuUpORDown = "up";
            }
            else
            {
                s_dianliuUpORDown = "down";
            }
            f_tempDianliu = f_startDianliu;
            f_endDianliuTemp = f_endDianliu;
            t_dianliuValue.text = f_tempDianliu.ToString("0.000");
        }
        else if (test == "终->始")
        {
            if (f_startDianliu < f_endDianliu)
            {
                s_dianliuUpORDown = "down";
            }
            else
            {
                s_dianliuUpORDown = "up";
            }
            f_tempDianliu = f_endDianliu;
            f_endDianliuTemp = f_startDianliu;

            t_dianliuValue.text = f_tempDianliu.ToString("0.000");
        }
        TimerMngr.Instance.Create(() => {
            b_isStart = true;                                   //电流开始随着时间的变化而变化
        }).Start(0.5f);
        OnllyUIController._instance.b_isDuihuakuang = false;
        ChangeMainPage(0);                                      //切换当前页面到第一页
        tf_allPage.GetChild(4).gameObject.SetActive(false);
        tf_bottomUI.Find("switch").GetChild(0).GetComponent<Image>().sprite = s_open;
        tf_bottomUI.Find("tishi").gameObject.SetActive(false);
        tf_bottomUI.Find("isStart").gameObject.SetActive(false); 
    }

    /// <summary>
    /// 手控确认开始试验
    /// </summary>
    void OnQueRenButtonClickShouKong() {
        OnllyPage5UIController.Instance.ClearJieGuo();
        OnllyPage1UIController.Instance.tf_right.Find("dongzuozhi").GetComponent<Text>().text = "";  //清空上一个试验的结果数据
        OnllyPage1UIController.Instance.tf_right.Find("fanhuizhi").GetComponent<Text>().text = "";
        OnllyPage1UIController.Instance.tf_right.Find("fanhuixishu").GetComponent<Text>().text = "";
        b_shoukongStart = true;
        go_chengkongSV.SetActive(false);
        go_shoukongSV.SetActive(true);
        OnllyUIController._instance.b_isDuihuakuang = false;
        tf_bottomUI.Find("tishi").gameObject.SetActive(false);
        tf_bottomUI.Find("isStart").gameObject.SetActive(false);
        OnllyPage1UIController.Instance.shoukongshiyan();
        tf_bottomUI.Find("switch").GetChild(0).GetComponent<Image>().sprite = s_open;
    }

    /// <summary>
    /// 改变Onlly设备的当前界面
    /// </summary>
    /// <param name="index">要切换的界面</param>
    void ChangeMainPage(int index) {
        tf_allPage.GetChild(OnllyUIController._instance.i_pageNum).gameObject.SetActive(false);
        OnllyUIController._instance.ChangeBottomUI(OnllyUIController._instance.i_pageNum, false);
        OnllyUIController._instance.i_pageNum = index;
        OnllyUIController._instance.ChangeBottomUI(OnllyUIController._instance.i_pageNum, true);
        tf_allPage.GetChild(OnllyUIController._instance.i_pageNum).gameObject.SetActive(true);
    }

    /// <summary>
    /// 是否正式开始试验取消按钮
    /// </summary>
    void OnQuXiaoButtonClick()
    {
        TimerMngr.Instance.Create(() => {
            OnllyUIController._instance.b_isDuihuakuang = false;
        }).Start(0.1f);              //防止此时对话框以外的交互进行
        tf_bottomUI.Find("tishi").gameObject.SetActive(false);
        tf_bottomUI.Find("isStart").gameObject.SetActive(false);
    }

    /// <summary>
    /// 3段电流是否都为“投入”状态
    /// </summary>
    /// <returns></returns>
    private bool is_AllTouru() {
        int sum = 0;
        for (int i = 3; i <= 5; i++)
        {
            if (YabanShezhiUIController._instance.tf_ContentRight.GetChild(i).GetChild(0).GetComponent<Text>().text == "投入")
            {
                sum++;
            }
        }
        if (sum == 3)
            return true;
        return false;
    }

    /// <summary>
    /// 旋转按钮事件
    /// </summary>
    /// <param name="dir"></param>
    void OnXuanzhuanAnniu(string dir) {
        if (OnllyUIController._instance.i_pageNum == 1 && !b_isStart)
        {
            if (dir == "shun")
            {
                OnRightButtonClick(true);
            }
            else if (dir == "ni")
            {
                OnDownButtonClick(true);
            }
        }    
    }

    /// <summary>
    /// 重置
    /// </summary>
    void OnReset() {

    }
}
